package com.framework.service.yhbservice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.user.YhbUserAddressDao;
import com.framework.exception.RRException;
import com.framework.exception.menu.RespCode;
import com.framework.service.yhbservice.YhbAddreesService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.string.StringUtil;
import com.framework.yhbEntity.user.YhbUser;
import com.framework.yhbEntity.user.YhbUserAddress;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service("yhbAddreesService")
public class YhbAddreesServiceImpl extends ServiceImpl<YhbUserAddressDao,YhbUserAddress> implements YhbAddreesService {

    @Autowired
    private YhbUserService yhbUserService;

    @Override
    public List<YhbUserAddress> getAddressByToken(String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);

        List<YhbUserAddress> list = baseMapper.selectList(new EntityWrapper<YhbUserAddress>().eq("user_id", user.getUserId()));

        return list;
    }

    @Override
    public void addAddress(YhbUserAddress address, String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        if(StringUtils.isBlank(address.getArea())||
                StringUtils.isBlank(address.getCity())||
                StringUtils.isBlank(address.getInfo())||
                StringUtils.isBlank(address.getNick())||
                StringUtils.isBlank(address.getProv())||
                address.getIsDefault() == null||
                StringUtils.isBlank(token)||
                address.getIsDefault() == null||
                StringUtils.isBlank(address.getPhone())){
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }

        String addressNo= StringUtil.getDateString(6);
        address.setUserId(user.getUserId());
        address.setCreateDate(new Date());
        address.setAddressId(addressNo);
        baseMapper.insert(address);


        //设置成默认
        if(address.getIsDefault() == 1){
            setDefault(addressNo);
        }else {
            YhbUserAddress ad = getDefaultAddreess(token);
            if(ad==null){
                setDefault(addressNo);
            }
        }

    }


    @Override
    public void setDefault(String addressNo){
        YhbUserAddress address = new YhbUserAddress();
        address.setAddressId(addressNo);
        YhbUserAddress sysAddress = baseMapper.selectOne(address);
        if(sysAddress != null){
            String userId = sysAddress.getUserId();
            if(userId!=null){
                baseMapper.updateDefaultByUserId(userId);
            }
            baseMapper.isDefaultById(addressNo);
        }
    }

    @Override
    public void updateAddress(YhbUserAddress address, String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        if(StringUtils.isBlank(address.getArea())||
                StringUtils.isBlank(address.getCity())||
                StringUtils.isBlank(address.getInfo())||
                StringUtils.isBlank(address.getNick())||
                StringUtils.isBlank(address.getProv())||
                StringUtils.isBlank(address.getAddressId())||
                StringUtils.isBlank(token)||
                address.getIsDefault() == null||
                StringUtils.isBlank(address.getPhone())){
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }

        String addressId = address.getAddressId();
        //修改之前校验
        infoAddress(token,addressId);
        baseMapper.updateById(address);
        //设置成默认
        if(address.getIsDefault() == 1){
            setDefault(addressId);
        }

    }

    @Override
    public YhbUserAddress infoAddress(String token, String addressId) {
        if(StringUtils.isBlank(token) ||
                StringUtils.isBlank(addressId)){
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }
        YhbUser user = yhbUserService.queryUserByToken(token);
        String userId = user.getUserId();
        YhbUserAddress address = new YhbUserAddress();
        address.setAddressId(addressId);
       YhbUserAddress sysAddress = baseMapper.selectOne(address);
        if(sysAddress == null){
            throw new RRException("9000","未查询到收货地址信息");
        }
        if(!userId.equals(sysAddress.getUserId())){
            throw new RRException("9000","此收货地址不属于当前登录人");
        }
        return sysAddress;
    }

    @Override
    public YhbUserAddress getDefaultAddreess(String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        YhbUserAddress address=new YhbUserAddress();
        address.setUserId(user.getUserId());
        address.setIsDefault(1);
        YhbUserAddress yhbUserAddress = baseMapper.selectOne(address);
        return yhbUserAddress;
    }

    @Override
    public void deleteAddress(String token, String addressId) {
        infoAddress(token,addressId);
        baseMapper.deleteById(addressId);
    }

    @Override
    public String addAddressByPhone(YhbUserAddress address) {

        if(StringUtils.isBlank(address.getArea())||
                StringUtils.isBlank(address.getCity())||
                StringUtils.isBlank(address.getInfo())||
                StringUtils.isBlank(address.getNick())||
                StringUtils.isBlank(address.getProv())||
                StringUtils.isBlank(address.getPhone())){
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }

        String addressNo= StringUtil.getDateString(6);
        address.setCreateDate(new Date());
        address.setAddressId(addressNo);
        baseMapper.insert(address);
        //设置成默认
        setDefault(addressNo);
        return addressNo;
    }

    @Override
    public YhbUserAddress queryAddressByPhone(String phone) {
        YhbUserAddress address=new YhbUserAddress();
        address.setPhone(phone);
        address.setIsDefault(1);
        return baseMapper.selectOne(address);
    }

}
